﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PowerBooking.RSE.Domain.Models;
using PowerBooking.RSE.Utility.Helpers;
using System.Text.RegularExpressions;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using System.Data.Entity;
using PowerBooking.RSE.Poco;
using System.Data.Objects;

namespace PowerBooking.RSE.Domain.Factories
{
    public class FilterFactory : Factory
    {
        public FilterFactory(ObjectContext objectContext) : base(objectContext) { }

        public List<Filter> CreateXiaomishuFilters(int cityID, string cityUrl)
        {
            var filters = new ConcurrentBag<Filter>();
            var url = cityUrl + "/sitemap";
            var html = HttpHelper.Get(url);

            var matches = Regex.Matches(html, GetXiaomishuSmallCategoryParttern(), RegexOptions.Singleline | RegexOptions.IgnoreCase).OfType<Match>();
            Parallel.ForEach(matches, (m, loopState) =>
            {

                if (!CacheHelper.Get<bool>("IsCaptureStop"))
                {
                    filters.Add(CreateFilter(cityID, cityUrl, m));
                }
                else
                {
                    loopState.Stop();
                    return;
                }

            });

            if (filters.Count == 0)
            {
                matches = Regex.Matches(html, GetXiaomishuBigCategoryParttern(), RegexOptions.Singleline | RegexOptions.IgnoreCase).OfType<Match>();

                Parallel.ForEach(matches, (m, loopState) =>
                {
                    if (!CacheHelper.Get<bool>("IsCaptureStop"))
                    {
                        filters.Add(CreateFilter(cityID, cityUrl, m));
                    }
                    else
                    {
                        loopState.Stop();
                        return;
                    }
                });
            }

            return filters.ToList();
        }

        private Filter CreateFilter(int cityID, string cityUrl, Match match)
        {
            var filterPoco = new FilterPoco()
            {
                Name = match.Groups[2].Value,
                Url = cityUrl + RegexHelper.CheckStringLastChar("/", match.Groups[1].Value),
                CityID = cityID
            };
            FilterRepository.InsertFilter(filterPoco);
            return new Filter().InitDomain(filterPoco);
        }

        private string GetXiaomishuSmallCategoryParttern()
        {
            var parttern = string.Empty;
            var attributes = RegexHelper.CreateAttribute("target", "_blank") + RegexHelper.Space + RegexHelper.CreateAttribute("href", @"(/shop/search/s" + RegexHelper.AnyAttributeValue + ")") + RegexHelper.Space + RegexHelper.CreateAttribute("class", "g3");
            return RegexHelper.CreateDom("a", attributes, RegexHelper.AnyDomValueInGroup);
        }

        private string GetXiaomishuBigCategoryParttern()
        {
            var parttern = string.Empty;
            var attributes = RegexHelper.CreateAttribute("target", "_blank") + RegexHelper.Space + RegexHelper.CreateAttribute("href", @"(/shop/search/s" + RegexHelper.AnyAttributeValue + ")") + RegexHelper.Space + RegexHelper.CreateAttribute("class", "b g3");
            return RegexHelper.CreateDom("a", attributes, RegexHelper.AnyDomValueInGroup);
        }


    }
}
